#include  "pit.h"
#include  "common.h"
#include "systick.h"
#include "gpio.h"
#include "adc.h"
#include "uart.h"
#include  "oled.h"
#include  "vcan_sccb.h"
#include  "dma.h"
#include "camera.h"
#include  "ov7725.h"
#include "fuzzy.h"
#include "ftm.h"
#include "path_recognition.h"
#include "steering.h"
#include "systick.h"
#include  "speed_control.h"
#include "support_senser.h"
#include "debuger.h"
#include "pid.h"
#include "bluetooth.h"



uint8_t control_flag=0;
float value_set=784;



//pit 5ms中断
void pit_isr()
{   
    control_flag=1; 
    //read_speed();
   
      
}


    
int main()
{//
    uint8_t p;
    SYSTICK_DelayInit();//初始化systick延时，oled使用
    oled_debug();
 
    SCCB_Init();//sccb引脚
    p=ov7725_eagle_reg_init();//鹰眼寄存器初始化
    while(!p); 
    ov7725_port_init(); //鹰眼管脚初始化
    //外设初始化
    UART_QuickInit( UART0_RX_PB16_TX_PB17,115200);//初始化uart0
	  DMA_UART_Init();//串口DMA 3.28 
    FTM_PWM_QuickInit(FTM2_CH0_PB18, kPWM_EdgeAligned,50);//舵机pwm初始化
    FTM_PWM_ChangeDuty(HW_FTM2,HW_FTM_CH0,784);
    Motor_PWM_Init();//初始化电机
     
    
    PIT_CallbackInstall(HW_PIT_CH0,pit_isr);//注册pit中断函数
    PIT_QuickInit(HW_PIT_CH0,5000);  //pit初始化5ms
    PIT_ITDMAConfig(HW_PIT_CH0, kPIT_IT_TOF,1);//开pit中断
    
    //编码器初始化
    read_speed_init();
   
   //拨码开关初始化
   boma_debug_init();
   read_boma();
   
    
    
    //中断优先级设置
    NVIC_SetPriorityGrouping(NVIC_PriorityGroup_2);
    NVIC_SetPriority(DMA0_IRQn, NVIC_EncodePriority(NVIC_PriorityGroup_2, 1, 1)); //dma中断优先级
    NVIC_SetPriority(PORTB_IRQn, NVIC_EncodePriority(NVIC_PriorityGroup_2, 2, 2));//场中断优先级
    NVIC_SetPriority(PIT0_IRQn,NVIC_EncodePriority(NVIC_PriorityGroup_2, 2, 1));//pit中断优先级
    
     /*  SPI_GPIO_init();
	    SD_Init();		
		//改为高速		//最高速只能到4分频
		SPI0->CTAR[0] &=~(SPI_CTAR_BR(SPI_SCK_DIV_8192));	//BR=32
		SPI0->CTAR[0] |=SPI_CTAR_BR(SPI_SCK_DIV_8);//8分频	*/
        
   //电机输出设置
	Set_Motor_PWM(left,2500);
    Set_Motor_PWM(right,2500);
    
    //蜂鸣器初始化
    buzzer_init();
    //adc初始化
    adc_init();
    
    //电池测试
    bat_test();
    
  
    
    
    //开始采图
    ov7725_eagle_get_img();
    
    
    while(1)
    
  {      
 
   
    
  //图像处理
       if(img_flag==1)
         {   img_flag=0;
             img_all_2();       
         }
        
        //出界停车
        if(stop_flag_2==1)
         {
           Set_Motor_PWM(left,0);
           Set_Motor_PWM(right,0); 
         }
        
        //控制
    if(control_flag==1&&stop_flag_2==0)
        {   
     
        control_flag=0;
        
    if(turn_left_flag_2==1||turn_right_flag_2==1)
            {
            
                   different_speed();
                  steering.bili_out=2.5;
                    Steering_FuzzyProcess(error_distance_2,derror_distance_2);
                    steering.out=smooth_process(steering.out);//打角滤波
							     FTM_PWM_ChangeDuty(HW_FTM2,HW_FTM_CH0,value_set);
                }

   else
             {       
                steering.bili_out=2;
                Steering_FuzzyProcess(error_distance_2,derror_distance_2);
                steering.out=smooth_process(steering.out); //打角滤波
                value_set=784+steering.out; 
                FTM_PWM_ChangeDuty(HW_FTM2,HW_FTM_CH0,value_set);
                Set_Motor_PWM(left,2500);
                Set_Motor_PWM(right,2500);
             


                }               
           // set_pid_speed();
        
         }            
    if(count_num>=8)
    {   
			  count_num=0;
				if(boma_state[0]==0)   
				{   
					SEND_IMG_flag=1;
					Reset_Send_img1buff(gCCDRAM);
					DMA_Restart_UART();
				}
     }	      
          
      //      
   }
        
  
}
    
    
   
 
  












